package elitech.vietnam.myfashion.dialog;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.wsclient.Utilities;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ForgotPasswordDialog extends Dialog {
	private Button mBtnCancel, mBtnCommit, mBtnClose;
	private EditText mTxtEmail;
	private TextView mTxtWarn;
	private Dialog mDialog;
	private LinearLayout mLayNormal, mLaySuccess;
	private ProgressBar mLoading;
	private ScrollView mScroll;
	
	public ForgotPasswordDialog(Context context) {
		super(context, R.style.Dialog);
		this.mDialog = this;
		setContentView(R.layout.forgotdialog);
		
		mLayNormal = (LinearLayout) findViewById(R.id.forgot_laynormal);
		mLaySuccess = (LinearLayout) findViewById(R.id.forgot_laysuccess);
		mTxtEmail = (EditText) findViewById(R.id.forgot_txtEmail);
		mTxtWarn = (TextView) findViewById(R.id.forgot_txtWarn);
		mBtnCommit = (Button) findViewById(R.id.forgot_btnCommit);
		mBtnCancel = (Button) findViewById(R.id.forgot_btnCancel);
		mBtnClose = (Button) findViewById(R.id.forgot_btnClose);
		mLoading = (ProgressBar) findViewById(R.id.forgot_loading);
		mScroll = (ScrollView) findViewById(R.id.scrollView1);
		
		mBtnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mDialog.dismiss();
			}
		});
		
		mBtnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mDialog.dismiss();
			}
		});
		
		mBtnCommit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				CommitEmail();
			}
		});
	}
	
	private void CommitEmail(){
		final String email = mTxtEmail.getText().toString();
		mTxtWarn.setText("");
		if(email.isEmpty()){
			mTxtWarn.setText(R.string.emailblank);
		}else if(!Utilities.checkEmailValid(email)) {
			mTxtWarn.setText(R.string.emailinvalid);
		}else{
			new CallServiceTask(){
				private int rs = -2;
				protected void onPreExecute() {
					mScroll.setVisibility(View.GONE);
					mLoading.setVisibility(View.VISIBLE);
				};
				public void work() {
					try {
						rs = service.forgotPassword(email);
					} catch (IOException e) {
						e.printStackTrace();
					} catch (XmlPullParserException e) {
						e.printStackTrace();
					}
				};
				protected void onPostExecute(Void result) {
					mScroll.setVisibility(View.VISIBLE);
					mLoading.setVisibility(View.GONE);
					if(rs==0){
						mTxtWarn.setText(R.string.emailnotexist);
					}else if(rs == 1){
						mLayNormal.setVisibility(View.GONE);
						mLaySuccess.setVisibility(View.VISIBLE);
					}else if(rs == -2){
						Log.w("Forgot", "Failed");
					}
				};
			}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	}
	
}
